package cn.tinlan.shopcoin.app.mine.adress;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.reflect.TypeToken;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallClazzProxy;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.subsciber.BaseSubscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.Utils.SPUtils;
import cn.tinlan.shopcoin.app.mine.adress.adapter.MultipleItemAdapter;
import cn.tinlan.shopcoin.app.mine.adress.model.AddressItemModel;
import cn.tinlan.shopcoin.base.BaseActivity;
import cn.tinlan.shopcoin.base.CustomApiResult;
import cn.tinlan.shopcoin.widget.category.Constants;
import io.reactivex.Observable;

/**
 * Created by Mortimer on 2018/12/19. 13:47
 * 佛祖🙏，永无bug。
 */
public class AddressListActivity extends BaseActivity implements View.OnClickListener{
    @BindView(R.id.address_list_title)
    TitleBar addressTitleBar;
    @BindView(R.id.address_list_rv)
    RecyclerView addressRV;

    public static final int REQUEST_CODE = 1347;


    private MultipleItemAdapter addressListAdapter;
    private List<AddressItemModel> addressItemModels;
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_address_list;
    }

    @Override
    public void initView() {
        super.initView();
        initTitleBar();
        initRV();
    }

    private void initTitleBar() {
        addressTitleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });
    }

    private void initRV() {
        if (addressListAdapter == null) {
            LinearLayoutManager llm = new LinearLayoutManager(AddressListActivity.this, LinearLayoutManager.VERTICAL, false);
            addressRV.setLayoutManager(llm);
            addressItemModels = new ArrayList<>();
            addressListAdapter = new MultipleItemAdapter(R.layout.view_address_item,R.layout.view_address_foot,addressItemModels);
            View view = getLayoutInflater().inflate(R.layout.view_address_foot,(ViewGroup) addressRV.getParent(), false);
            View foot = view.findViewById(R.id.address_list_foot);
            foot.setOnClickListener(this);
            addressListAdapter.addFooterView(view);
            addressRV.setAdapter(addressListAdapter);
        }else {
            addressListAdapter.notifyDataSetChanged();
        }


    }


    @Override
    public void initData() {
        super.initData();
        loadAddressListData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.address_list_foot:
                gotoAddressAEActivity();
                break;
        }
    }


    private void gotoAddressAEActivity() {
        Intent intent = new Intent(AddressListActivity.this, AddressActivity.class);
        intent.putExtra("id","");
        intent.putExtra("up_address",false);
        startActivityForResult(intent,REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE && resultCode == 1346) {
            Log.d("反向传值","success");
        }
    }

    private void loadAddressListData() {

        Observable<List<AddressItemModel>> observable = EasyHttp.get(Constants.UserAddressList)
                .params("user_id", SPUtils.getUserID(AddressListActivity.this))
                .execute(new CallClazzProxy<CustomApiResult<List<AddressItemModel>>, List<AddressItemModel>>(new TypeToken<List<AddressItemModel>>() {}.getType()) {
                });

        observable.subscribe(new BaseSubscriber<List<AddressItemModel>>() {
            @Override
            public void onError(ApiException e) {


            }

            @Override
            public void onNext(List<AddressItemModel> AddressItemModels) {
                super.onNext(AddressItemModels);

                addressItemModels = AddressItemModels;
                if (addressItemModels.size() > 0){
                    addressListAdapter.setNewData(addressItemModels);
                }else {
                    gotoAddressAEActivity();
                }

            }
        });
    }
}
